/**
 * Created by 小敏哥 on 2018/1/18.
 */
//获取应用实例
var app = getApp();
Component({
    properties: {
        index: {
            type: Number,
        },
        addressId: {
            type: String,
        },
        name: {
            type: String,
        },

        phone: {
            type: String,
        },

        default: {
            type: Boolean,
        },

        address: {
            type: String,
        },
        mailboxNum: {
            type: String,
        },
        showAddress: {
            type: Boolean,
        },
        provinceId: {
            type: String,
        },
        provinceName: {
            type: String,
        },
        cityId: {
            type: String,
        },
        cityName: {
            type: String,
        },
        countyId: {
            type: String,
        },
        countyName: {
            type: String,
        },
        street:{
            type:String,
        },
        //是否显示默认地址选项
        showTick: {
            type: Boolean, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
            value: true, // 属性初始值（可选），如果未指定则会根据类型选择一个
        },
        //是否选中
        tick: {
            type: Boolean, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
            value: false, // 属性初始值（可选），如果未指定则会根据类型选择一个
        },
        remark:{
            type:String,
        }
    },
    data: {},
    methods: {
        //跳转到编辑
        toEdit() {
            if(this.data.showAddress) {
                let data = this.data;
                delete data.tick;
                delete data.showTick;
                delete data.default;
                app.setStorageSync('editEMSAddressData', data);
                app.navigateTo({url: '../addAddress/addAddress'});
            }
            else{
                app.setStorageSync('mailboxNumData', {id:this.data.addressId,remark:this.data.remark,mailboxNum:this.data.mailboxNum});
                app.navigateTo({url: '../editMailboxNum/editMailboxNum'});
            }
        },

        contentClick(){
            var myEventDetail = {
                active: this.data.index
            };// detail对象，提供给事件监听函数
            var myEventOption = {}; // 触发事件的选项
            this.triggerEvent('choose', myEventDetail, myEventOption)
        },
        //设置默认地址
        setActive(e) {
            let opt = {
                url: app.globalData.host + "/wx/ad/default.ajax",
                data: {
                    id: this.data.addressId
                },
                sucFn: (result) => {
                    if (result.code == 1000) {
                        var myEventDetail = {
                            active: this.data.index
                        };// detail对象，提供给事件监听函数
                        var myEventOption = {}; // 触发事件的选项
                        this.triggerEvent('actvechange', myEventDetail, myEventOption)
                    }
                    else {
                        wx.showToast({
                            title: result.data.msg,
                            icon: 'none',
                            duration: 2000
                        });
                    }
                }
            };
            app.ajax(opt);
        },
        //删除地址
        deleteAddress() {
            wx.showModal({
                title: '提示',
                content: '确定要删除' + this.data.name + "的地址",
                success: (res) => {
                    if (res.confirm) {
                        let opt = {
                            url: app.globalData.host + "/wx/ad/dl.ajax",
                            data: {
                                id: this.data.addressId
                            },
                            sucFn: (result) => {
                                if (result.code == 1000) {
                                    this.triggerEvent('shoudReload', {}, {});
                                    //发送删除成功事件
                                    var myEventDetail = {
                                        addressId: this.data.addressId
                                    };// detail对象，提供给事件监听函数
                                    var myEventOption = {}; // 触发事件的选项
                                    this.triggerEvent('deleteSuccess', myEventDetail, myEventOption)
                                }
                                else {
                                    wx.showToast({
                                        title: result.data.msg,
                                        icon: 'none',
                                        duration: 2000
                                    });
                                }
                            }
                        };
                        app.ajax(opt);
                    }
                }
            })
        }
    },
});